package cn.view;

import cn.comon.RedisUtil;
import cn.comon.UserSession;
import cn.dao.EmployeeDao;
import cn.entity.Employee;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class LoginView extends JFrame {

    private JPanel eidPanel;
    private JPanel epwPanel;
    private JPanel buttonPanel;
    private JPanel headlinePanel;

    private JTextField eidText;
    private JPasswordField epwText;

    private Font font;

    public LoginView() {
        this.setDisplay();

        getContentPane().add(headlinePanel);
        getContentPane().add(eidPanel);
        getContentPane().add(epwPanel);
        getContentPane().add(buttonPanel);

        this.setTitle("超市管理系统");
        this.setSize(500, 300);
        getContentPane().setLayout(new GridLayout(4, 1));
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        // 点击关闭按钮时退出程序，将点击关闭按钮为隐藏窗口的默认操作改为退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void setDisplay() {
        this.setPanel();
        this.setFont();
        this.setLabel();
        this.setButton();
        this.setTextField();
    }

    public void setTextField() {
        eidText = new JTextField(12);
        eidText.setFont(font);

        epwText = new JPasswordField(12);
        epwText.setFont(font);
        epwText.setEchoChar('*');

        epwText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent ee) {
                if (ee.getKeyCode() == KeyEvent.VK_ENTER) {
                    checkEmp();
                }
            }
        });
        eidPanel.add(eidText);
        epwPanel.add(epwText);
    }

    public void setPanel() {
        this.eidPanel = new JPanel();
        this.eidPanel.setLayout(new FlowLayout());
        this.epwPanel = new JPanel();
        this.epwPanel.setLayout(new FlowLayout());
        this.buttonPanel = new JPanel();
        this.buttonPanel.setLayout(new FlowLayout());
        this.headlinePanel = new JPanel();
        this.headlinePanel.setLayout(new FlowLayout());
    }

    public void setFont() {
        font = new Font("华文宋体", Font.PLAIN, 20);
    }

    public void setLabel() {
        JLabel eidLabel = new JLabel("员工号：");
        eidLabel.setFont(font);
        eidPanel.add(eidLabel);

        JLabel epwLabel = new JLabel("密  码：");
        epwLabel.setFont(font);
        epwPanel.add(epwLabel);

        JLabel headline = new JLabel("超市管理系统");
        headline.setFont(new Font("华文中宋", Font.BOLD, 40));
        headlinePanel.add(headline);
    }

    public void setButton() {
        JButton submit = new JButton("提交");
        submit.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                checkEmp();
            }
        });
        JButton exit = new JButton("退出系统");
        exit.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }
        });
        buttonPanel.add(submit);
        buttonPanel.add(exit);
    }

    public void checkEmp() {
        String eid = eidText.getText();
        String epw = new String(epwText.getPassword());

        Employee retrievedEmp;

        try (RedisUtil redis = new RedisUtil()) {
            String cachedPassword = redis.get(eid);
            if (cachedPassword != null && cachedPassword.equals(epw)) {
                try (EmployeeDao empDB = new EmployeeDao()) {
                    retrievedEmp = empDB.checkByEidAndEPas(eid, epw);
                }
                if (retrievedEmp != null) {
                    enterNextPage(retrievedEmp);
                    return;
                }
            }
        }

        try (EmployeeDao empDB = new EmployeeDao()) {
            retrievedEmp = empDB.checkByEidAndEPas(eid, epw);
            if (retrievedEmp != null) {
                try (RedisUtil redis = new RedisUtil()) {
                    redis.set(eid, epw);
                }
                enterNextPage(retrievedEmp);
            } else {
                JOptionPane.showMessageDialog(null, "用户名不存在或密码不正确", "登陆错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void enterNextPage(Employee emp) {
        if (emp != null) {
            UserSession.getInstance().setCurrentUser(emp);  // 保存当前用户信息
            if (emp.getElevel().equals("00")||emp.getElevel().equals("10")) {
                new AdminView(emp);
            } else if (emp.getElevel().equals("20")) {
                new UserView(emp);
            }
            dispose();  // 关闭登录窗口
        } else {
            JOptionPane.showMessageDialog(null, "未能成功登录", "登录错误", JOptionPane.ERROR_MESSAGE);
        }
    }



}
